package com.ruoyi.jjkc.jjkcsq.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.jjkc.jjkcsq.domain.Jjkcsq;
import com.ruoyi.jjkc.jjkcsq.domain.JjkcsqFj;
import com.ruoyi.jjkc.jjkcsq.mapper.JjkcsqMapper;
import com.ruoyi.jjkc.jjkcsq.service.IJjkcsqService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 加计扣除申请Service业务层处理
 *
 * @author huacai
 * @date 2024-12-08
 */
@Service
public class JjkcsqServiceImpl implements IJjkcsqService {
    @Resource
    private JjkcsqMapper jjkcsqMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询加计扣除申请
     *
     * @param jjkcsqId 加计扣除申请主键
     * @return 加计扣除申请
     */
    @Override
    public Jjkcsq selectJjkcsqByJjkcsqId(String jjkcsqId) {
        return jjkcsqMapper.selectJjkcsqByJjkcsqId(jjkcsqId);
    }

    /**
     * 查询加计扣除申请列表
     *
     * @param jjkcsq 加计扣除申请
     * @return 加计扣除申请
     */
    @Override
    public List<Jjkcsq> selectJjkcsqList(Jjkcsq jjkcsq) {
        return jjkcsqMapper.selectJjkcsqList(jjkcsq);
    }

    /**
     * 新增加计扣除申请
     *
     * @param jjkcsq 加计扣除申请
     * @return 结果
     */
    @Transactional
    @Override
    public int insertJjkcsq(Jjkcsq jjkcsq) {
        jjkcsq.setCreateTime(DateUtils.getNowDate());
        int rows = jjkcsqMapper.insertJjkcsq(jjkcsq);
        insertJjkcsqFj(jjkcsq);
        return rows;
    }

    /**
     * 批量新增加计扣除申请
     *
     * @param jjkcsqs 加计扣除申请List
     * @return 结果
     */
    @Override
    public int batchInsertJjkcsq(List<Jjkcsq> jjkcsqs) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(jjkcsqs)) {
            try {
                for (int i = 0; i < jjkcsqs.size(); i++) {
                    int row = jjkcsqMapper.insertJjkcsq(jjkcsqs.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 100 == 0) || i == jjkcsqs.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改加计扣除申请
     *
     * @param jjkcsq 加计扣除申请
     * @return 结果
     */
    @Transactional
    @Override
    public int updateJjkcsq(Jjkcsq jjkcsq) {
        jjkcsq.setUpdateTime(DateUtils.getNowDate());
//        jjkcsqMapper.deleteJjkcsqFjByJjkcsqId(jjkcsq.getJjkcsqId());
        insertJjkcsqFj(jjkcsq);
        return jjkcsqMapper.updateJjkcsq(jjkcsq);
    }

    /**
     * 批量删除加计扣除申请
     *
     * @param jjkcsqIds 需要删除的加计扣除申请主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteJjkcsqByJjkcsqIds(String[] jjkcsqIds) {
        jjkcsqMapper.deleteJjkcsqFjByJjkcsqIds(jjkcsqIds);
        return jjkcsqMapper.deleteJjkcsqByJjkcsqIds(jjkcsqIds);
    }

    /**
     * 删除加计扣除申请信息
     *
     * @param jjkcsqId 加计扣除申请主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteJjkcsqByJjkcsqId(String jjkcsqId) {
        jjkcsqMapper.deleteJjkcsqFjByJjkcsqId(jjkcsqId);
        return jjkcsqMapper.deleteJjkcsqByJjkcsqId(jjkcsqId);
    }

    /**
     * 新增附件
     * @param jjkcsqFj
     * @return
     */
    @Override
    public int insertJjkcsqFjByJjkcsqId(JjkcsqFj jjkcsqFj) {
        return jjkcsqMapper.insertJjkcsqFjByJjkcsqId(jjkcsqFj);
    }

    /**
     * 根据ID查询附件列表
     * @param jjkcsqFj
     * @return
     */
    @Override
    public List<JjkcsqFj> selectJjkcsqFjList(JjkcsqFj jjkcsqFj) {
        return jjkcsqMapper.selectJjkcsqFjList(jjkcsqFj);
    }

    /**
     * 删除加计扣除申请附件
     * @param fjIds
     * @return
     */
    @Override
    public int deleteJjkcsqFjByFjIds(Integer[] fjIds) {
        return jjkcsqMapper.deleteJjkcsqFjByFjIds(fjIds);
    }

    /**
     * 新增加计扣除申请附件信息
     *
     * @param jjkcsq 加计扣除申请对象
     */
    public void insertJjkcsqFj(Jjkcsq jjkcsq) {
        List<JjkcsqFj> jjkcsqFjList = jjkcsq.getJjkcsqFjList();
        String jjkcsqId = jjkcsq.getJjkcsqId();
        if (StringUtils.isNotNull(jjkcsqFjList)) {
            List<JjkcsqFj> list = new ArrayList<JjkcsqFj>();
            for (JjkcsqFj jjkcsqFj : jjkcsqFjList) {
                jjkcsqFj.setJjkcsqId(jjkcsqId);
                list.add(jjkcsqFj);
            }
//            if (list.size() > 0) {
//                jjkcsqMapper.batchJjkcsqFj(list);
//            }
        }
    }
}
